import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import javax.swing.border.EmptyBorder;



/**
 * Represents the map screen. Shows the current progress of the wagon visually.
 *
 */
public class MapPanel extends JPanel implements Serializable{
	
	private Game game;
	private Map map;
	
	int maxIndices = Map.Landmarks.length;
	
	private JButton btnCloseButton;

	
	public MapPanel(Game g, Map m){
		
		//FIXME shouldn't have access to map (ideally)
		game = g;
		map = m;
		
		setPreferredSize(new Dimension(800,600));
		
		setLayout(null);
		setBorder(new EmptyBorder(5, 5, 5, 5));
		
		btnCloseButton = new JButton("Back");
		btnCloseButton.setBounds(649, 35, 103, 25);
		add(btnCloseButton);
		btnCloseButton.addActionListener(new CloseButtonListener());
		
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		Image trailbg = new ImageIcon("trailbg.jpg").getImage();
		
		//Draws the Background
		g.drawImage(trailbg, 0, 0, null);
		
		int landmarkIndex = map.getLandmarkIndex();
		
		//Draws all the lines
		g.setColor(Color.black);
		for (int i=1; i<=maxIndices; i++){
			
			if (landmarkIndex >= i) {
				drawPath(g, (i-1));
			}
			else {
				double percentageTraveledBetweenLM = 
				  ((double)map.getSinceLastLandmark() 
					 / (double)Map.Landmarks[i-1].getDistBetweenLandmarks());
				
				drawPartialPath(g, (i-1), percentageTraveledBetweenLM);
				break;
			}
		}
	}
		
	
	/**
	 * Draws a path between two landmarks. The index of the first landmark to
	 * draw is given by startIndex, and the index of the second landmark is
	 * calculated by adding 1 to the startIndex.
	 * 
	 * @param g
	 * @param startIndex
	 */
	public void drawPath(Graphics g, int startIndex){
		
		int x1 = Map.Landmarks[startIndex].getX();
		int y1 = Map.Landmarks[startIndex].getY();
		
		int x2 = Map.Landmarks[startIndex + 1].getX();
		int y2 = Map.Landmarks[startIndex + 1].getY();
		
		g.drawLine(x1, y1, x2, y2);
		
		g.drawLine(x1, (y1-1), x2, (y2-1));
		g.drawLine(x1, (y1+1), x2, (y2+1));
		
		g.drawLine((x1-1), y1, (x2-1), y2);
		g.drawLine((x1+1), y1, (x2+1), y2);
	}
	
	
	
	/**
	 * Draws a path between a Landmark and the wagon's current location when it has not
	 * yet reached the next Landmark. Also draws the wagon at the end of that path.
	 * 
	 * @param g - the Graphics object to draw on
	 * @param startIndex - index of the Landmark that the Wagon has already passed
	 * @param howfar - a decimal between 0 and 1 representing the approximate
	 * 			percentage of the path that has been completed between the
	 * 			landmarks.
	 */
	public void drawPartialPath(Graphics g, int startIndex, double howfar){
		
		Image tinywagon = new ImageIcon("tinywagon.jpg").getImage();
		
		int x1 = Map.Landmarks[startIndex].getX();
		int y1 = Map.Landmarks[startIndex].getY();
		
		
		int xToNextLM = Map.Landmarks[startIndex + 1].getX() - x1;
		int yToNextLM = Map.Landmarks[startIndex + 1].getY() - y1;
		
		int x2 = (int) ((xToNextLM * howfar) + x1);
		int y2 = (int) ((yToNextLM * howfar) + y1);
		
		g.drawLine(x1, y1, x2, y2);
		
		g.drawLine(x1, (y1-1), x2, (y2-1));
		g.drawLine(x1, (y1+1), x2, (y2+1));
		
		g.drawLine((x1-1), y1, (x2-1), y2);
		g.drawLine((x1+1), y1, (x2+1), y2);
		
		g.drawImage(tinywagon, (x2-9), (y2-7), null);
		
	}
		

	
	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Map Window");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		
		Map map = new Map();
		map.move(50);
		map.move(50);
		map.move(50);
		//map.move(50);
		//map.move(50);
		
		MapPanel mp = new MapPanel(new Game(), map);
		//map.setVisible(true);
		frame.getContentPane().add(mp);

		frame.pack();
		frame.setVisible(true);
	}
	
	
	class CloseButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			game.changePanel("TurnPanel");
		}
	}
	
}
	
	
	

